package player.ui.browse;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.TreePath;

import net.miginfocom.swing.MigLayout;
import player.PS;
import player.Tools;
import player.ui.playlist.PlayListManage;
import player.ui.playlist.PlayListModel;
import player.ui.tree.FileSystemTreeRendererVideo;
import player.ui.tree.FileTree;
import player.ui.video.TPanel;
import player.ui.video.TranPanel;

public class BrowsePanel extends JComponent{
	private static final long serialVersionUID = -8178377007394661L;
	
	MyJSplitPane videoJSplitPane1;
	MyJSplitPane MyJSplitPane2;
	FileListPanel fileListPanel;
	PlayListPanel PlayListPanel;
	
	int state_showBrowse=1;
	int state_showPlaylistBrowse=2;
	int state_closeBrowse=3;
	int state=state_showBrowse;
	
	int la=0;

	TranPanel browseTPanel;
	TranPanel playListTPanel;
	TranPanel closeTPanel;
	FileTree tree;
	PlayListManage pm;
	public BrowsePanel(PlayListManage pm){
		super();
		this.pm=pm;
		//setLayout(new MigLayout("insets 0 0 0 0"));
		setLayout(new ALayout());
		videoJSplitPane1=new MyJSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		videoJSplitPane1.setBorder(null);
		PS.videoJSplitPane1=videoJSplitPane1;

		setBorder(Tools.creatDefaultBorder());
		browseTPanel=Tools.creatTranPanel();
		browseTPanel.setBorder(null);
		playListTPanel=Tools.creatTranPanel();
		closeTPanel=Tools.creatTranPanel();
		add(browseTPanel);
		add(playListTPanel);
		add(closeTPanel);
		
		browseTPanel.add(videoJSplitPane1,"width :10000: ,height :10000: ");
		videoJSplitPane1.setDividerLocation(PS.config.l5Location);

		JScrollPane jsp=new JScrollPane();
		videoJSplitPane1.setLeftComponent(jsp);
		jsp.setBorder(null);
		
		tree=new FileTree();
		tree.setCellRenderer(new FileSystemTreeRendererVideo());
		tree.setOpaque(false);
		tree.setUI(new BasicTreeUI());
		jsp.setViewportView(tree);
		tree.setBorder(null);
		tree.setRootVisible(false);
		TreePath path = tree.getPathForRow(0);
		tree.expandPath(path);
		path = tree.getPathForRow(2);
		tree.expandPath(path);
		tree.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				int row = tree.getRowForLocation(e.getX(), e.getY());
				TreePath path =tree. getPathForRow(row);
				if(path!=null){
					//tree.expandPath(path);
					File dir= (File) path.getLastPathComponent();
					openDir(dir);
					//System.out.println("KKKKKKKKKKKKK  "+dir.getAbsolutePath());
					PS.config.lastOpenFileTreeDirectory=Tools.path2String(path);
					PS.config.lastOpenDirectory=dir.getAbsolutePath();
					PS.saveConfig();
				}
			}
		});
		tree.expandPath(Tools.string2Path(tree.treeModel, PS.config.lastOpenFileTreeDirectory));
		
		
		jsp.getViewport().setOpaque( false );
		jsp.setOpaque( false );
		jsp.getVerticalScrollBar().setBorder(null);
		jsp.getVerticalScrollBar().setPreferredSize(new Dimension(0,100));
		jsp.getHorizontalScrollBar().setBorder(null);
		jsp.getHorizontalScrollBar().setPreferredSize(new Dimension(100,7));
		
		MyJSplitPane2=new MyJSplitPane(JSplitPane.VERTICAL_SPLIT);
		MyJSplitPane2.setBorder(null);
		MyJSplitPane2.setDividerSize(0);
		videoJSplitPane1.setRightComponent(MyJSplitPane2);
		MyJSplitPane2.setDividerLocation(PS.config.l5Location);
		
		fileListPanel=new FileListPanel();
		PS.FileBrowsePanel=fileListPanel.ImgPanel;
		MyJSplitPane2.setTopComponent(fileListPanel);
		
		
		PlayListPanel=new PlayListPanel();
		//MyJSplitPane2.setBottomComponent(PlayListPanel);
	}
	
	void openDir(File dir){
		String name=dir.getAbsolutePath();
		if(!PS.FileListManage.containsPlayList(name)){
			PS.FileListManage.addFolderPlayerList(name,dir);
		}
		PlayListModel plm=PS.FileListManage.getPlayListModel(name);
		System.out.println("EEEEEEEEEE "+dir.getAbsolutePath()+" "+plm.getList().size());
		fileListPanel.openDir(pm,plm,new File(PS.config.lastOpenDirectory));
	}
	
	
	public void showBrowse(){
		if(state!=state_showBrowse){
			state=state_showBrowse;
			MyJSplitPane2.setDividerLocation(la);
			playListTPanel.setVisible(false);
			browseTPanel.setVisible(true);
			validate();
			repaint();
		}else{
			closeBrowse();
		}
		
	}
	
	public void showPlaylistBrowse(){
		if(state!=state_showPlaylistBrowse){
			if(state==state_showBrowse){
				la=MyJSplitPane2.getDividerLocation();
			}
			state=state_showPlaylistBrowse;
			playListTPanel.add(PlayListPanel,"width :10000: ,height :10000: ");
			browseTPanel.setVisible(false);
			playListTPanel.setVisible(true);
			validate();
			repaint();
		}else{
			closeBrowse();
		}
	
	}
	
	public void closeBrowse(){
		if(state!=state_closeBrowse){
			if(state==state_showBrowse){
				la=MyJSplitPane2.getDividerLocation();
			}
			
			state=state_closeBrowse;
			playListTPanel.setVisible(false);
			browseTPanel.setVisible(false);
			validate();
			repaint();
		}
		
	}
}
